/** layuiAdmin.std-v1.2.1 LPPL License By http://www.layui.com/admin/ */
layui.define(['form','table',"element",'pkc','laypage','format'],function (e) {
    var t = layui.$;
    var layform = layui.form;
    var laytable = layui.table;
    var setter = layui.setter;
    var r = (layui.form,layui.element);
    var laypage = layui.laypage;
    var format = layui.format;
    var pkc = layui.pkc;

    var form = {
        formData: null,
        //加载表格
        initGrid: function(){
            laytable.init('LAY-user-back-manage',{
                elem: "#LAY-user-back-manage",
                id: "LAY-user-back-manage",
                cellMinWidth: 80,
                cols: [[{
                    type: "checkbox"
                },{
                    field: "parkingname",
                    width: "120",
                    title: "停车场名称",
                    align: "center"
                },{
                    field: "parkingid",
                    width: "120",
                    title: "停车场id",
                    align: "center"
                },{
                    field: "exitname",
                    width: "120",
                    title: "车道名称",
                    align: "center"
                },{
                    field: "signkey",
                    width: "200",
                    title: "key",
                    align: "center"
                },{
                    field: "createtime",
                    width: "200",
                    title: "创建时间",
                    align: "center",
                    templet: "#SexTpl"
                },{
                    field: "modifytime",
                    width: "200",
                    title: "修改时间",
                    align: "center"
                },{
                    field: "createip",
                    width: "140",
                    title: "创建ip",
                    align: "center"
                },{
                    field: "modifyip",
                    width: "120",
                    title: "修改ip",
                    align: "center"
                },{
                    field: "status",
                    width: "100",
                    title: "状态",
                    align: "center",
                    templet: "#StatusTpl"
                },{
                    title:"操作",
                    width: "200",
                    align:"center",
                    fixed: "right",
                    templet: "#table-system-order"
                }]],
                toolbar: "#toolbar",
                defaultToolbar:[''],
                text: {
                    none: "暂无相关数据"
                },
                done: (res, curr, count) => {
                    t(window).resize();
                    r.render("progress");
                    count || t('div.layui-table-box>div').css('overflow', 'auto');
                }
            })
        },
        /**
         * 初始化分页
         * @param {number} count 总数
         * @param {number} pindex 当前页
         */
        initPage: function (count, pindex) {
            laypage.render({
            elem: "apppage",
            count: count,
            layout: ['limit', 'prev', 'page', 'next', 'count', 'refresh', 'skip'],
            jump: function (obj, first) {
                //obj包含了当前分页的所有参数，比如：
                // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                // console.log(obj.limit); //得到每页显示的条数
    
                //首次不执行
                if (!first) {
                //do something
                form.loadData(obj.limit, obj.curr);
                }
            },
            curr: pindex
            });
        },
        //表格数据加载
        loadData: function(cp, pc){
            layer.load("1");
            var data = form.getParame(form.formData); //查询条件
            data["pagesize"] = cp;
            data["pageindex"] = pc;
            return pkc.http.SendPost(data,'psamcloud.boss.device.lane.list', function(ret){
                var ret = JSON.parse(ret);
                if(ret !== null && ret.Status === "1"){
                    // 加载表格
                    laytable.reload("LAY-user-back-manage", {
                        data: ret.Data.rows
                    });
                    laytable.resize("LAY-user-back-manage");
                    //加载分页
                    form.initPage(ret.Data.total, pc);
                }else{
                    layer.alert(ret.Message);
                }
                setTimeout(() => {
                    layer.closeAll("loading");
                }, 1000);
            });            
        },
        // 事件监听
        initEvt: function(){
            //搜索
            layform.on('submit(LAY-user-back-search)', function(data){
                //执行重载
                layer.load(2);
                form.formData = data;
                form.loadData(10, 1);
                laytable.reload('LAY-user-back-manage', {
                  where: form.formData
                });
            });
            
            laytable.on("toolbar(LAY-user-back-manage)", function(e){   
                var checkdata = laytable.checkStatus(e.config.id).data;             
                //新增
                if(e.event === "add"){
                    layer.open({
                        id: "add",
                        type: 2,
                        title: "新增车道",
                        content: "laneadd.html",
                        area: ["600px", "450px"],
                        success: function (layero, i) {
                            window.reloadGrid = form.loadData;
                        }
                    });
                }
                //发送邮件
                if(e.event === 'sendemail'){
                    if(checkdata == [] || checkdata.length == 0){
                        layer.msg("请选择要发送的记录");
                        return;
                    }
                    layer.open({
                        id: "mail",
                        type: 2,
                        title: "发送邮件",
                        content: "lanesendmail.html",
                        area: ["600px", "250px"],
                        success: function (layero, i) {
                            window.checkdata = checkdata;
                            window.reloadGrid = form.loadData;
                        }
                    });
                }
            });
            //编辑 & 删除
            laytable.on("tool(LAY-user-back-manage)", function(e){
                window.datas = e.data;  
                var id = e.data.id;              
                if(e.event === "edit"){
                    layer.open({
                        id: "edit",
                        type: 2,
                        title: "修改车道",
                        content: "laneedit.html",
                        area: ["600px", "500px"],
                        success: function (layero, i) {
                            window.reloadGrid = form.loadData;
                        }
                    });
                };
                if(e.event === 'del'){
                    layer.confirm("确定需要删除该数据?", {
                        icon: 3,
                        title: "提示"
                    }, function (index) {
                        // form.toolAction.del(id).done(() => {
                        //     layer.close(index);
                        // });
                        var data = {};
                        data["id"] = id;
                        return pkc.http.SendPost(data,'psamcloud.boss.device.lane.delete',function(ret){
                            var ret = JSON.parse(ret);
                            if(ret){
                                if (ret.Status === "1") {
                                    //success
                                    layer.msg('操作成功');
                                    form.close();
                                    //重新加载数据
                                    form.loadData(10, 1);
                                } else {
                                    layer.alert(ret.Message);
                                }
                            }
                        },function(ret){
                            layer.alert("网络异常.请稍后再试！");
                        })
                    });
                }
            });
        },
        //页面初始化
        init: function(){
            form.initGrid();
            form.initEvt();
            form.initPage();
        },
        //获取参数
        getParame: function(fmData){
            var data = {};
            data["parkingname"] = fmData.field.parkingname;
            data["exitname"] = fmData.field.exitname;
            data["signkey"] = fmData.field.signkey;
            data["status"] = (fmData.field.status!=='')?parseInt(fmData.field.status):'';
            data["createtime1"] = fmData.field.createtime1 + ' 00:00:00';
            data["createtime2"] = fmData.field.createtime2 + ' 23:59:59';
            return data;
        },
        //关闭窗体
        close: function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    }

    e("laneinfo", {
        init: () => {
            form.init();
        }
    });
});